package com.dayuanit.dy8.atm.springbootatm.service;

import com.dayuanit.dy8.atm.springbootatm.entity.User;
import com.dayuanit.dy8.atm.springbootatm.entity.WxBingInfo;
import com.dayuanit.dy8.atm.springbootatm.exception.BizException;
import com.dayuanit.dy8.atm.springbootatm.mapper.UserMapper;
import com.dayuanit.dy8.atm.springbootatm.mapper.WxBingInfoMapper;
import com.dayuanit.dy8.authen.wxatuehnspringbootstart.holder.WxUserInfoHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WxBindService {

    @Autowired
    private WxBingInfoMapper wxBingInfoMapper;

    @Autowired
    private UserMapper userMapper;

    public User saveWxInfo(WxUserInfoHolder infoHolder) {
        final WxBingInfo info = new WxBingInfo();
        info.setCity(infoHolder.getCity());
        info.setCountry(infoHolder.getCountry());
        info.setHeadimgurl(infoHolder.getHeadimgurl());
        info.setNickname(infoHolder.getNickname());
        info.setOpenid(infoHolder.getOpenid());
        info.setProvince(infoHolder.getProvince());
        info.setSex(infoHolder.getSex());
        info.setUnionid(infoHolder.getUnionid());

        //先检查绑定表是否有记录 有则更新 无则添加

        User user = null;

        WxBingInfo wxBindInfo = wxBingInfoMapper.getWxBindInfo(info.getOpenid());
        if (null == wxBindInfo) {
            int rows = wxBingInfoMapper.insert(info);
            if (1 != rows) {
                throw new BizException("微信登录失败");
            }
        } else {

            if (null != wxBindInfo.getUserId()) {
                user = userMapper.selectByPrimaryKey(wxBindInfo.getUserId());
            }

            wxBindInfo.setUnionid(info.getUnionid());
            wxBindInfo.setSex(info.getSex());
            wxBindInfo.setProvince(info.getProvince());
            wxBindInfo.setOpenid(info.getOpenid());
            wxBindInfo.setNickname(info.getNickname());
            wxBindInfo.setHeadimgurl(info.getHeadimgurl());
            wxBindInfo.setCountry(info.getCountry());
            wxBindInfo.setCity(info.getCity());

            int rows = wxBingInfoMapper.updateByPrimaryKey(wxBindInfo);
            if (1 != rows) {
                throw new BizException("微信登录失败");
            }
        }

        return user;
    }

    public User processBind(String username, String pwd, String openId) {
        //校验用户是否存在 密码是否正确
        User user = userMapper.getUser(username);
        if (null == user) {
            throw new BizException("用户不存在或者密码错误");
        }

        if (!user.getPassword().equals(pwd)) {
            throw new BizException("用户不存在或者密码错误");
        }

        //将userID存放到绑定表里
        final WxBingInfo wxBindInfo = wxBingInfoMapper.getWxBindInfo(openId);
        if (null == wxBindInfo) {
            throw new BizException("非法请求");
        }

        wxBindInfo.setUserId(user.getId());
        final int rows = wxBingInfoMapper.updateByPrimaryKey(wxBindInfo);
        if (1 != rows) {
            throw new BizException("绑定失败");
        }

        return user;
    }
}
